package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.enums.JobStatus;
import com.zhilei.exceptions.GraceException;
import com.zhilei.grace.result.ResponseStatusEnum;
import com.zhilei.intercept.JWTCurrentUserInterceptor;
import com.zhilei.mapper.JobMapper;
import com.zhilei.pojo.Job;
import com.zhilei.pojo.Users;
import com.zhilei.pojo.bo.EditJobBO;
import com.zhilei.service.JobService;
import com.zhilei.untils.GsonUtils;
import com.zhilei.untils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 志磊
 */
@Slf4j
@Service
public class JobServiceImpl extends BaseInfoProperties implements JobService {

    @Autowired
    private JobMapper jobMapper;

    /**
     * 发布职位信息
     *
     * @param editJobBO 职位信息BO类
     */
    @Transactional
    @Override
    public void modify(EditJobBO editJobBO) {

        String id = editJobBO.getId();
        Job job = new Job();
        BeanUtils.copyProperties(editJobBO, job);
        job.setUpdatedTime(LocalDateTime.now());

        if (StringUtils.isBlank(id)) {
            // 新增操作
            job.setStatus(JobStatus.OPEN.type);
            job.setCreateTime(LocalDateTime.now());
            jobMapper.insert(job);
        } else {
            // 编辑操作
            jobMapper.update(job, new UpdateWrapper<Job>()
                    .eq("id", id)
                    .eq("hr_id", editJobBO.getHrId())
                    .eq("company_id", editJobBO.getCompanyId()));
        }

        redis.del(REDIS_JOB_DETAIL + ":" + editJobBO.getCompanyId() + ":" + editJobBO.getHrId() + ":" + editJobBO.getId());

    }

    /**
     * 查询发布的职位
     *
     * @param hrId
     * @param companyId
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PagedGridResult getJobList(String hrId, String companyId, Integer page, Integer pageSize, Integer status) {

        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        PageHelper.startPage(page, pageSize);

        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(hrId)) {
            jobQueryWrapper.eq("hr_id", hrId);
        }

        jobQueryWrapper.eq("company_id", companyId);

        if (status != null) {
            if (status != 0) {
                jobQueryWrapper.eq("status", status);
            }
        }

        jobQueryWrapper.orderByDesc("updated_time");
        List<Job> jobs = jobMapper.selectList(jobQueryWrapper);

        // 1.
        

        PagedGridResult pagedGridResult = setterPagedGrid(jobs, page);

        return pagedGridResult;
    }

    /**
     * 查询职位详情信息
     *
     * @param hrId
     * @param companyId
     * @param jobId
     * @return
     */
    @Override
    public Job jobDetail(String hrId, String companyId, String jobId) {

        // TODO 在redis中查询
        String jobDetailStr = redis.get(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId);
        Job job;
        if (StringUtils.isBlank(jobDetailStr)) {
            // TODO 在redis中查询不到，就在数据库中查询，并放到redis中
            job = this.queryJobDetail(hrId, companyId, jobId);
        } else {
            // TODO 直接从redis中取，返回给前端
            job = GsonUtils.stringToBean(jobDetailStr, Job.class);
        }

//        if (StringUtils.isBlank(companyId) || StringUtils.isBlank(jobId)) {
//            GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);
//        }
//        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
//
//        if (StringUtils.isNotBlank(hrId)) {
//            jobQueryWrapper.eq("hr_id", hrId);
//        }
//
//        jobQueryWrapper.eq("company_id", companyId);
//        jobQueryWrapper.eq("id", jobId);
//
//        Job job = jobMapper.selectOne(jobQueryWrapper);

        return job;

    }

    /**
     * 封装公共的查询方法
     *
     * @param hrId
     * @param companyId
     * @param jobId
     * @return
     */
    public Job queryJobDetail(String hrId, String companyId, String jobId) {

        // TODO 职位的不同状态
        Integer[] status = {1, 2, 3};

        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("id", jobId);

        if (StringUtils.isNotBlank(hrId)) {
            jobQueryWrapper.eq("hr_id", hrId);
        }

        if (StringUtils.isNotBlank(companyId)) {
            jobQueryWrapper.eq("company_id", companyId);
        }

        jobQueryWrapper.in("status", status);
        jobQueryWrapper.orderByDesc("updated_time");

        // TODO 查询一个对象
        Job job = jobMapper.selectOne(jobQueryWrapper);

        // TODO 放到redis中
        redis.set(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId, GsonUtils.object2String(job));

        return job;
    }


    /**
     * 关闭职位信息
     *
     * @param hrId
     * @param companyId
     * @param jobId
     * @return
     */
    @Transactional
    @Override
    public void close(String hrId, String companyId, String jobId) {

        if (StringUtils.isBlank(companyId) || StringUtils.isBlank(jobId) || StringUtils.isBlank(hrId)) {
            GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        Job job = new Job();
        job.setStatus(JobStatus.CLOSE.type);
        job.setUpdatedTime(LocalDateTime.now());

        UpdateWrapper<Job> jobUpdateWrapper = new UpdateWrapper<>();

        if (StringUtils.isNotBlank(hrId)) {
            jobUpdateWrapper.eq("hr_id", hrId);
        }

        jobUpdateWrapper.eq("company_id", companyId);
        jobUpdateWrapper.eq("id", jobId);
        jobUpdateWrapper.orderByDesc("updated_time");

        jobMapper.update(job, jobUpdateWrapper);

        redis.del(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId);

    }

    /**
     * 开放职位信息
     *
     * @param hrId
     * @param companyId
     * @param jobId
     * @return
     */
    @Transactional
    @Override
    public void open(String hrId, String companyId, String jobId) {
        if (StringUtils.isBlank(companyId) || StringUtils.isBlank(jobId) || StringUtils.isBlank(hrId)) {
            GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        Job job = new Job();
        job.setStatus(JobStatus.OPEN.type);
        job.setUpdatedTime(LocalDateTime.now());

        UpdateWrapper<Job> jobUpdateWrapper = new UpdateWrapper<>();

        if (StringUtils.isNotBlank(hrId)) {
            jobUpdateWrapper.eq("hr_id", hrId);
        }

        jobUpdateWrapper.eq("company_id", companyId);
        jobUpdateWrapper.eq("id", jobId);
        jobUpdateWrapper.orderByDesc("updated_time");

        jobMapper.update(job, jobUpdateWrapper);
    }

    /**
     * 历史职位
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedGridResult jobList(Integer page, Integer pageSize) {
        // TODO 实现历史职位列表的查询
        if (page == 0) {
            page = 1;
        }

        if (pageSize == 0) {
            pageSize = 10;
        }

        Users users = JWTCurrentUserInterceptor.USER_THREAD.get();
        String hrInWhichCompanyId = users.getHrInWhichCompanyId();
        PagedGridResult jobList = this.getJobList(null, hrInWhichCompanyId, page, pageSize, null);
        return jobList;
    }

}



























